package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.bo.WithdrawalHistoryBO;
import com.junzhiit.nengji.shop.dao.WithdrawalHistoryDAO;
import com.junzhiit.nengji.shop.model.WithdrawalHistory;
import com.junzhiit.nengji.shop.model.enums.BankCardType;
import com.junzhiit.nengji.shop.model.enums.WithdrawalStatus;

@Repository
public class WithdrawalHistoryDAOImpl extends AbstractBaseDAO<WithdrawalHistory> implements WithdrawalHistoryDAO {

	@Override
	public List<WithdrawalHistoryBO> findAll(WithdrawalHistory shopWithdrawalHistory, int pageNumber,
			int maxPageRow) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select wh.*,bc.bank_card,bc.bank,bc.bank_type,bc.bank_address,bc.company_name,bc.status 'bank_status'");
		
		if(shopWithdrawalHistory.getType() == BankCardType.SHOP.toIntValue()){
			sql.append(",(select shop.name from shop where wh.shop_id=shop.id) 'shop_name'");
		}
		
		sql.append(" from withdrawal_history wh left join bank_card bc on wh.bank_id = bc.id");
		sql.append(" where wh.is_delete = ?");
		StatementParameter param=new StatementParameter();
		
		param.setInt(IsDelete.NO.toIntValue());
		if(StringUtils.isNotBlank(shopWithdrawalHistory.getProposer())){
			sql.append(" and wh.proposer like ?");
			param.setString("%" + shopWithdrawalHistory.getProposer() + "%");
		}
		
		if(StringUtils.isNotBlank(shopWithdrawalHistory.getWithdrawalNumber())){
			sql.append(" and wh.withdrawal_number like ?");
			param.setString("%" + shopWithdrawalHistory.getWithdrawalNumber() + "%");
		}
		
		if(shopWithdrawalHistory.getStatus() != null){
			sql.append(" and wh.status = ?");
			param.setInt(shopWithdrawalHistory.getStatus());
		}
		
		if(shopWithdrawalHistory.getType() != null){
			sql.append(" and wh.type = ?");
			param.setInt(shopWithdrawalHistory.getType());
		}
		
		if(StringUtils.isNotBlank(shopWithdrawalHistory.getShopId())){
			sql.append(" and wh.shop_id = ?");
			param.setString(shopWithdrawalHistory.getShopId());
		}
		
		if(StringUtils.isNotBlank(shopWithdrawalHistory.getUserId())){
			sql.append(" and wh.user_id = ?");
			param.setString(shopWithdrawalHistory.getUserId());
		}
		
		sql.append(" order by wh.created_time desc");
		
		return super.queryForPagingList(sql.toString(), WithdrawalHistoryBO.class, param, pageNumber, maxPageRow);
	}

	@Override
	public int getTotalCount(WithdrawalHistory shopWithdrawalHistory) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(wh.id)");
		sql.append(" from withdrawal_history wh left join bank_card bc on wh.bank_id = bc.id");
		sql.append(" where wh.is_delete = ?");
		
		StatementParameter param=new StatementParameter();
		
		param.setInt(IsDelete.NO.toIntValue());
		if(StringUtils.isNotBlank(shopWithdrawalHistory.getProposer())){
			sql.append(" and wh.proposer = ?");
			param.setString("%" + shopWithdrawalHistory.getProposer() + "%");
		}
		
		if(StringUtils.isNotBlank(shopWithdrawalHistory.getWithdrawalNumber())){
			sql.append(" and wh.withdrawal_number like ?");
			param.setString("%" + shopWithdrawalHistory.getWithdrawalNumber() + "%");
		}
		
		if(shopWithdrawalHistory.getStatus() != null){
			sql.append(" and wh.status = ?");
			param.setInt(shopWithdrawalHistory.getStatus());
		}
		
		if(shopWithdrawalHistory.getType() != null){
			sql.append(" and wh.type = ?");
			param.setInt(shopWithdrawalHistory.getType());
		}
		
		if(StringUtils.isNotBlank(shopWithdrawalHistory.getShopId())){
			sql.append(" and wh.shop_id = ?");
			param.setString(shopWithdrawalHistory.getShopId());
		}
		
		if(StringUtils.isNotBlank(shopWithdrawalHistory.getUserId())){
			sql.append(" and wh.user_id = ?");
			param.setString(shopWithdrawalHistory.getUserId());
		}
		
		return super.queryForInt(sql.toString(),param);
	}

	@Override
	public int getWaitAuditCount() {
		String sql="select count(*) from withdrawal_history where is_delete = ? and status = ?";
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(WithdrawalStatus.WAIT.toIntValue());
		return super.queryForInt(sql.toString(),param);
	}
}
